package com.apchernykh.rozhlasapp.services;

import com.apchernykh.rozhlasapp.player.IPlaybackService;
import com.apchernykh.rozhlasapp.player.NullPlaybackService;
import com.apchernykh.rozhlasapp.services.BackgroundPlaybackService.LocalBinder;
import com.apchernykh.rozhlasapp.ui.RozhlasAppBaseFragmentActivity;
import com.apchernykh.rozhlasapp.utils.ConditionalLog;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;

public class ServiceMaster {
	public IPlaybackService mService;
	public boolean mBound;
	/** Defines callbacks for service binding, passed to bindService() */
	public ServiceConnection mConnection = new ServiceConnection() {

		@Override
		public void onServiceConnected(ComponentName iClassName, IBinder iService) {
			// We've bound to LocalService, cast the IBinder and get
			// LocalService instance
			LocalBinder aBinder = (LocalBinder) iService;
			mService = aBinder.getService();
			RozhlasAppBaseFragmentActivity.mApplicationModel.mBackgroundPlaybackService = mService;
			RozhlasAppBaseFragmentActivity.mApplicationModel.mInetInfo = aBinder
					.getService();
			// RozhlasAppBaseFragmentActivity.mApplicationModel.mStationSelected
			// = 0;
			// mService.startPlayback(new
			// NullPlaybackService.DetailedUrlStub());
			// mService.enableBackgroundPlayback();

			mBound = true;
			ConditionalLog.e("Service new connection "
					+ String.valueOf(mService));

			mService.reScheduleViewExpiration(Math.max(
					System.currentTimeMillis() + 5000,
					RozhlasAppBaseFragmentActivity.mApplicationModel.getExpirationTime()));

		}

		@Override
		// TODO: find out why this method is never called.
		public void onServiceDisconnected(ComponentName iArg0) {
			// ConditionalLog.e("Service disconnected");
			if (mService == RozhlasAppBaseFragmentActivity.mApplicationModel.mBackgroundPlaybackService) {
				mService = new NullPlaybackService();
				RozhlasAppBaseFragmentActivity.mApplicationModel.mBackgroundPlaybackService = mService;
			}
			mService = null;
			mBound = false;
		}
	};

	public ServiceMaster(IPlaybackService iService, boolean iBound) {
		this.mService = iService;
		this.mBound = iBound;
	}

	public void bind(Context iContext) {
		Intent aIntent = new Intent(iContext, BackgroundPlaybackService.class);
		if (!mBound) {
			ConditionalLog.e("Want to bind connection "
					+ String.valueOf(mConnection));
			iContext.bindService(aIntent, mConnection, Context.BIND_AUTO_CREATE);
			mBound = true;
		}
	}

	public void unbind(final Context iContext) {
//		try {
//			BackgroundPlaybackService.createKeepaliveIntent(context).send();
//		} catch (CanceledException e) {
//			ConditionalLog.e("Could not keepalive ");
//		}
		mService.cancelViewExpiration();
		if (mBound) {
			// Unbind from the service
			iContext.unbindService(mConnection);
			mBound = false;
			ConditionalLog.e("Unbound connection "
					+ String.valueOf(mConnection));
		} else {
			ConditionalLog.e("Connection seems to be not bound anymore... "
					+ String.valueOf(mConnection));
		}
	}
}